package com.igeek.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.boot.entity.User;
import com.igeek.boot.mapper.UserMapper;
import com.igeek.boot.service.UserService;
import com.igeek.boot.vo.UserQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @package com.igeek.boot.controller
 * @Description User类的Controller
 * @Author LittlePunk [296526219@qq.ccom]
 * @Version 2022.1
 * @Date 2023/12/12 12:37
 */

/**
 * Swagger2 API接口文档
 * 1.用于前后人员的交接
 * 2.完成接口测试
 *
 * 常用注解：
 * 1.@ApiModel("用户信息")         用在实体类（VO、QueryVO、DTO、PO等）上
 * 2.@ApiModelProperty("用户编号") 用在实体类（VO、QueryVO、DTO、PO等）的属性上
 * 3.@Api(tags = "用户的控制层")   用在Controller类上
 * 4.@ApiOperation("通过用户编号查询用户信息") 用在Controller类中的方法上
 * 5.@ApiParam("用户编号")         用在Controller类中的方法的形参列表上
 *
 * Swagger2 测试地址
 * http://localhost:8080/swagger-ui.html
 *
 * Knife4j 测试地址
 * http://localhost:8899/doc.html#/home
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口测试类")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("根据id查询用户信息")
    @GetMapping("{id}")
    public User findOne(@ApiParam("用户id") @PathVariable Long id){
        User user = userService.getById(id);
        return user;
    }

    //查询所有列表数据
    @ApiOperation(("查询用户表所有数据"))
    @GetMapping("findAll")
    public List<User> findAll(){
        List<User> list = userService.list();
        return list;
    }

    //条件+分页查询数据
    @ApiOperation("通过分页条件和关键字进行分页查询")
    @PostMapping("findAll/{current}/{size}")
    public Page<User> findAll(
            @ApiParam("传递的页码") @PathVariable(value = "current") Integer current,
            @ApiParam("传递的查询条数") @PathVariable(value = "size") Integer size,
            @ApiParam("传递查询的用户信息关键字") @RequestBody UserQueryVO userQueryVO
            ){
        //1.封装Page对象

        Page<User> page = new Page<>();

        //封装查询条件
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //第一个参数：boolean值，true则拼接条件   第二个参数：字段名，Lambda表达式   第三个参数：实际传递的数据
        lambdaQueryWrapper
                .like(StringUtils.hasLength(userQueryVO.getName()),User::getName,userQueryVO.getName())
                .or()
                .like(StringUtils.hasLength(userQueryVO.getEmail()),User::getEmail,userQueryVO.getEmail());
        //3.执行查询
        Page<User> userPage = userService.page(page,lambdaQueryWrapper);
        log.info("userPage:{}" , userPage);
        log.info("记录:{}" , userPage.getRecords());
        log.info("当前页码:{}" , userPage.getCurrent());
        log.info("每页展示的记录数:{}" , userPage.getSize());
        log.info("总记录数:{}" , userPage.getTotal());
        log.info("总页数:{}" , userPage.getPages());
        return userPage;
    }

    //执行添加用户信息
    @PostMapping("add")
    @ApiOperation("添加用户信息")
    public boolean addUser(@ApiParam("传递的user信息") @RequestBody User user){
        //插入用户信息
        boolean flag = userService.save(user);
        return flag;
    }

    //删除操作
    @DeleteMapping("del/{id}")
    @ApiOperation("根据id删除用户信息")
    public boolean del(@ApiParam("传递的用户id") @PathVariable("id") Long id){
        //物理删除  delete from user where id = ?
        //逻辑删除  update user set is_deleted =1 where id = ? and is_deleted = 0;
        boolean flag = userService.removeById(id);
        return flag;
    }

    /**
     * 乐观锁
     * UPDATE user SET user_name=?, age=?, email=?, update_time=?, version=? WHERE id=? AND version=? AND is_deleted=0
     */
    //修改
    @ApiOperation("修改用户信息")
    @PutMapping("update")
    public boolean update(@ApiParam("user用户信息（json数据）") @RequestBody User user){
        boolean flag = userService.updateById(user);
        return flag;
    }

    //希尔排序
    public static void shellSort(int[] arr) {
        int n = arr.length;
        int gap = n / 2;

        while (gap > 0) {
            for (int i = gap; i < n; i++) {
                int temp = arr[i];
                int j = i;

                while (j >= gap && arr[j - gap] > temp) {
                    arr[j] = arr[j - gap];
                    j -= gap;
                }

                arr[j] = temp;
            }

            gap /= 4;
        }
    }


}
